//
//  HBDiscoverItemRequest.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBDiscoverItemRequest.h"

@implementation HBDiscoverItemRequest
#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(回调一个数组)success failed:(失败回调)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.haveMore = YES;
    self.page = 1;
    [HBRequest loadChannelItemWithChannelId:self.channelId channelType:self.channelType success:^(NSArray *array) {
        self.channelArray = array;
        [HBRequest loadForYouListWithChannel_type:self.channelType page:self.page success:^(NSArray *array) {
            [self loadSuccess:success withArray:array];
        } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
            failed(response, error, responseObject);
            self.loading = NO;
        }];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(回调一个数组)success failed:(失败回调)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [HBRequest loadForYouListWithChannel_type:self.channelType page:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(回调一个数组)success withArray:(NSArray *)array {
    self.haveMore = array.count >= 10;
    self.page++;
    self.loading = NO;
    success(array);
}
@end
